package com.steven.im.net;

import android.app.Activity;
import android.content.Intent;

import com.steven.im.base.Constant;
import com.steven.im.ui.activity.ImLoginActivity;
import com.steven.im.utils.SaveParameterUtils;
import com.steven.im.utils.UserUtils;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Steven on 2019/1/24.
 */
public abstract class MyCallback<T> implements Callback<T> {
    private Activity activity;

    protected MyCallback(Activity activity) {
        this.activity = activity;
    }

    @Override
    public void onResponse(Call call, Response response) {
        SaveParameterUtils.saveToken(response.raw().header(Constant.CommonInfo.TOKEN));
        if (response.code() == 401) {

            UserUtils.clearUserInfo(); //等于401的时候得清除所有的用户信息
            activity.startActivity(new Intent(activity, ImLoginActivity.class));
        } else if (response.code() == 200) {
            onSuccess(response);
        }
    }

    @Override
    public void onFailure(Call call, Throwable t) {
        onFailure(t);
    }

    protected abstract void onSuccess(Response<T> response);


    protected abstract void onFailure(Throwable t);
}
